(function(){
	$('#menu_setting')[0].onclick = function (){
		if($('#menu_setting').hasClass('selected')) return;
		if($('.menu li.selected')[0]) $('.menu li.selected')[0].className = "";
		$('#menu_setting').addClass('selected');
		$('.main-content div').addClass('hidden');
		$('#content-setting')[0].className = '';
		load_setting();
		if(mobile) $('.sidebar').fadeOut();
	}
	$('#menu_user')[0].onclick = function (){
		if($('#menu_user').hasClass('selected')) return;
		if($('.menu li.selected')[0]) $('.menu li.selected')[0].className = "";
		$('#menu_user').addClass('selected');
		$('.main-content div').addClass('hidden');
		$('#content-user')[0].className = '';
		load_user();
		if(mobile) $('.sidebar').fadeOut();
	}
	$('#menu_new')[0].onclick = function (){
		if($('#menu_new').hasClass('selected')) return;
		if($('.menu li.selected')[0]) $('.menu li.selected')[0].className = "";
		$('#menu_new').addClass('selected');
		$('.main-content div').addClass('hidden');
		$('#content-new')[0].className = '';
		load_new();
		if(mobile) $('.sidebar').fadeOut();
	}
	$('#menu_list')[0].onclick = function (){
		if($('#menu_list').hasClass('selected')) return;
		if($('.menu li.selected')[0]) $('.menu li.selected')[0].className = "";
		$('#menu_list').addClass('selected');
		$('.main-content div').addClass('hidden');
		$('#content-list')[0].className = '';
		load_list();
		if(mobile) $('.sidebar').fadeOut();
	}
	function load_setting(){
		$('.loading-icon').finish();
		$('.loading-icon').fadeIn();
		$.getJSON("admin.php?action=load_setting", function(result){
			$('.loading-icon').finish();
			$('.loading-icon').fadeOut();
			if(!result) return;
			$('#notice')[0].value = result.notice ? result.notice : '';
			$('#block_register')[0].checked = result.block_register ? 'checked' : '';
			$('#invite_code')[0].value = result.invite_code ? result.invite_code : '';
			$('#block_upload')[0].checked = result.block_upload ? 'checked' : '';
			$('#block_download')[0].checked = result.block_download ? 'checked' : '';
			$('#quality_1')[0].value = result.quality_1 ? result.quality_1 : '';
			$('#quality_2')[0].value = result.quality_2 ? result.quality_2 : '';
			$('#quality_3')[0].value = result.quality_3 ? result.quality_3 : '';
			$('#block_vdisk')[0].checked = result.block_vdisk ? 'checked' : '';
			$('#block_baidupcs')[0].checked = result.block_baidupcs ? 'checked' : '';
			$('#block_vdisk_reason')[0].value = result.block_vdisk_reason ? result.block_vdisk_reason : '';
			$('#block_baidupcs_reason')[0].value = result.block_baidupcs_reason ? result.block_baidupcs_reason : '';
		});
	}
	function load_user(){
		$('.loading-icon').finish();
		$('.loading-icon').fadeIn();
		$.getJSON("admin.php?action=load_user", function(result){
			$('.loading-icon').finish();
			$('.loading-icon').fadeOut();
			if(!result) return;
			$('#content-user table tbody')[0].innerHTML = '';
			$.each(result, function(i, field){
				$("#content-user table tbody").append("<tr><td>"+field.uid+"</td><td>"+field.username+"</td><td>"+field.email+"</td><td><a href=\"admin.php?action=deluser&uid="+field.uid+"&formhash="+formhash+"\" onclick=\"return msg_win_action(this.href)\">删除用户</a></td></tr>");
			});
		});
	}
	function load_new(){
		$('.loading-icon').finish();
		$('.loading-icon').fadeIn();
		$.getJSON("admin.php?action=load_new", function(result){
			$('.loading-icon').finish();
			$('.loading-icon').fadeOut();
			if(!result) return;
			$('#content-new table tbody')[0].innerHTML = '';
			$.each(result, function(i, field){
				$("#content-new table tbody").append("<tr><td>"+field.id+"</td><td>"+field.uid+"</td><td>"+field.title+"</td><td>"+field.artist+"</td><td>"+field.album+"</td><td>"+field.type+"</td><td><a onclick=\"window.open('play.swf?src="+siteurl+"/file/link/"+field.uid+"/"+field.id+".mp3&auto_play=true','newwindow','height=400,width=700,top=200,left=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')\">试听</a> | <a href=\"admin.php?action=pass&formhash="+formhash+"&id="+field.id+"\">通过</a> | <a href=\"admin.php?action=delsong&formhash="+formhash+"&id="+field.id+"\" onclick=\"if(!confirm('确认要删除吗？')) return false;\">删除</a></td></tr>");
			});
		});
	}
	function load_list(){
		$('.loading-icon').finish();
		$('.loading-icon').fadeIn();
		$.getJSON("admin.php?action=load_list", function(result){
			$('.loading-icon').finish();
			$('.loading-icon').fadeOut();
			if(!result) return;
			$('#content-list table tbody')[0].innerHTML = '';
			$.each(result, function(i, field){
				$("#content-list table tbody").append("<tr><td>"+field.id+"</td><td>"+field.uid+"</td><td>"+field.title+"</td><td>"+field.artist+"</td><td>"+field.album+"</td><td>"+field.type+"</td><td><a onclick=\"window.open('play.swf?src="+siteurl+"/file/link/"+field.uid+"/"+field.id+".mp3&auto_play=true','newwindow','height=400,width=700,top=200,left=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')\">试听</a> | <a href=\"admin.php?action=delsong&formhash="+formhash+"&id="+field.id+"\" onclick=\"if(!confirm('确认要删除吗？')) return false;\">删除</a></td></tr>");
			});
		});
	}
	function parse_hash(){
		var hash = location.hash.substring(1);
		if(hash == "setting"){
			$('#menu_setting')[0].onclick();
		}else if(hash == "user"){
			$('#menu_user')[0].onclick();
		}else if(hash == "new"){
			$('#menu_new')[0].onclick();
		}else if(hash == "list"){
			$('#menu_list')[0].onclick();
		}else{
			$('#menu_setting')[0].onclick();
		}
	}
	$('.menubtn').click(function(){
		$('.sidebar').fadeToggle();
	});
	$(window).on('hashchange', function() {
		parse_hash();
	});
	parse_hash();
})();